<?php
/**
 * Author: lf
 * Blog: https://blog.szdma.com
 * Email: job@szdma.com
 * Created at: 2016-03-21 19:55.
 */

namespace backend\grid;

use kartik\grid\ActionColumn as kartikActionColumn;
use Yii;
use yii\helpers\Html;
use yii\helpers\Url;

/**
 * {@inheritdoc}
 */
class ActionColumn extends kartikActionColumn
{
    public $Options = [];

    public $width = '';

    /**
     * {@inheritdoc}
     */
    public function init()
    {
        $this->initMyButtons();

        $this->header = yii::t('app', $this->header);
        if (!isset($this->headerOptions['width'])) {
            $this->headerOptions['width'] = $this->width;
        }
        $this->contentOptions = ['class' => 'da-icon-column', 'style' => 'width:'.$this->width.';'];

        parent::init();
    }

    /**
     * @Notes   按钮初始化
     * @Interface initMyButtons
     *
     *
     * @Author Aaron/刘志勇
     * @Email lzymail@foxmail.com
     * @Date 2020-01-11 23:09
     */
    protected function initMyButtons()
    {
        $this->setButton('view', Yii::t('kvgrid', 'View'), ['iconOptions' => ['class' => 'glyphicon glyphicon-eye-open']]);
        $this->setButton('update', Yii::t('kvgrid', 'Update'), ['iconOptions' => ['class' => 'glyphicon glyphicon-pencil'],'reload'=>'true']);
        $this->setButton('comment', Yii::t('app', 'Comments'), ['iconOptions' => ['class' => 'fa fa-comments fa-lg']]);
        $this->setButton('setauth', Yii::t('app', 'Setauth'), ['iconOptions' => ['class' => 'glyphicon glyphicon-pencil']]);   //功能授权按钮
        $this->setButton('dataauth', Yii::t('app', 'Dataauth'), ['iconOptions' => ['class' => 'glyphicon glyphicon-pencil']]); //数据授权按钮
    }

    /**
     * 表格设置普通自定义按钮.
     *
     * @param $name
     * @param $title
     * @param $iconOptions
     */
    protected function setButton($name, $title, $options)
    {
        if (isset($this->buttons[$name])) {
            return;
        }
        $this->buttons[$name] = function ($url) use ($name, $title, $options) {
            $_options = [
                'title'      => $title,
                'aria-label' => $title,
                'data-pjax'  => '0',
                'data-url'   => $url,
                'data-title' => $title,
                'class'      => ' btn-n-sm  J_menuItem layer-window',
            ];
            $options  = array_replace_recursive($options, $_options, $this->buttonOptions, $this->Options);
            $label    = $this->renderLabel($options, $title, $options['iconOptions']);

            $link = Html::a($label, 'javascript:void(0)', $options);

            if ($this->_isDropdown) {
                $options['tabindex'] = '-1';

                return "<li>{$link}</li>\n";
            } else {
                return $link;
            }
        };
    }
}
